package main

import (
	"fmt"
	mqtt "github.com/eclipse/paho.mqtt.golang"
	"time"
)

var messagePubHandler mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message) {
	fmt.Printf("Received message: %s from topic: %s\n", msg.Payload(), msg.Topic())
}

var connectHandler mqtt.OnConnectHandler = func(client mqtt.Client) {
	fmt.Println("Connected")
}

var connectLostHandler mqtt.ConnectionLostHandler = func(client mqtt.Client, err error) {
	fmt.Printf("Connect lost: %v", err)
}

func main() {
	var broker = "127.0.0.1"
	var port = 1883
	opts := mqtt.NewClientOptions()
	opts.AddBroker(fmt.Sprintf("mqtt://%s:%d", broker, port))
	opts.SetClientID("go_mqtt_client")
	opts.SetUsername("ziu")
	opts.SetPassword("zxcvbnm,./123")
	//opts.SetDefaultPublishHandler(messagePubHandler)
	opts.OnConnect = connectHandler
	opts.OnConnectionLost = connectLostHandler
	client := mqtt.NewClient(opts)
	if token := client.Connect(); token.Wait() && token.Error() != nil {
		panic(token.Error())
	}

	// sub(client)
	publish(client)

	client.Disconnect(250)
}

func publish(client mqtt.Client) {
	//num := 10
	//for i := 0; i < num; i++ {
	//	text := fmt.Sprintf("Message %d", i)
	//	token := client.Publish("/server/task/roleId/1/update_task_data", 0, false, text)
	//	token.Wait()
	//	time.Sleep(time.Second)
	//}

	text := fmt.Sprintf("Message --")
	token := client.Publish("xiaomi/home1/control/led/off", 0, false, text)
	token.Wait()
	time.Sleep(time.Second)
}

func sub(client mqtt.Client) {
	topic := "/server/task/roleId/1/update_task_data"
	token := client.Subscribe(topic, 1, nil)
	token.Wait()
	fmt.Printf("Subscribed to topic: %s", topic)
}
